/*
* Copyright (C) 2009-2012 - Roman Kalynchuk
*
* This file is part of REngine.
*
* REngine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REngine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REngine.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __VERTEXFORMAT_H__
#define __VERTEXFORMAT_H__

#include "kernel/kerneltypes.h"
#include "render/rendertypes.h"

namespace rengine3d {

	//! usages for vertex element
	enum VertexElementUsage
	{
		POSITION = 1,
		NORMAL = 2,
		COLOR = 3,
		TANGENT = 4,
		BINORMAL = 5,
		BLENDWEIGHTS = 6,
		BLENDINDICES = 7,
		TEXCOORD0 = 8,
		TEXCOORD1 = 9,
		TEXCOORD2 = 10,
		TEXCOORD3 = 11,
		TEXCOORD4 = 12,
		TEXCOORD5 = 13,
		TEXCOORD6 = 14,
		TEXCOORD7 = 15
	};

	//! vertex element
	class CVertexElement {
	public:
		CVertexElement();
		CVertexElement(uint size, VertexElementUsage usage);
	public:
		uint				m_size;
		VertexElementUsage	m_usage;
	};

	r_inline CVertexElement::CVertexElement(): m_size(0), m_usage(POSITION) {
	}

	r_inline CVertexElement::CVertexElement(uint size, VertexElementUsage usage): m_size(size), m_usage(usage) {
	}

	//! vertex format
	class CVertexFormat {
	public:
		CVertexFormat();
		CVertexFormat(const CVertexElement* elements, uint elementsCount);
		~CVertexFormat();

		uint GetVertexSize()	{ return m_vertexSize; };
		uint GetElementCount()	{ return m_elements.size(); };
		const CVertexElement& GetElement(uint ind) { return m_elements[ind]; };
	private:
		std::vector<CVertexElement> m_elements;
		uint						m_vertexSize;
	};

	r_inline CVertexFormat::CVertexFormat(): m_vertexSize(0) {
	}

	r_inline CVertexFormat::CVertexFormat(const CVertexElement* elements, uint elementsCount): m_vertexSize(0) {
		for (uint i = 0; i < elementsCount; ++i) {
			CVertexElement element;
			memcpy(&element, &elements[i], sizeof(CVertexElement));
			m_elements.push_back(element);
			// TODO: maybe use own type 'real' ???
			m_vertexSize += element.m_size + sizeof(float);
		}
	}

	r_inline CVertexFormat::~CVertexFormat() {
		m_elements.clear();
	}

}

#endif